#pragma once
#pragma warning (disable: 4996)

#include <string>
#include <vector>
#include <list>
#include <fstream>
#include "BasicFunction.h"
#include "afxcmn.h"
#include "TextProcess.h"
#include "SearchImage.h"

static unsigned SCREEN_DIMX = ::GetSystemMetrics(SM_CXSCREEN);
static unsigned SCREEN_DIMY = ::GetSystemMetrics(SM_CYSCREEN);

struct FILELIST
{
	std::string dir;
	std::vector<std::string> files;
	std::vector<__time64_t> times;
};

// CAnnotationTool dialog
class CAnnotationTool : public CDialog
{
	DECLARE_DYNAMIC(CAnnotationTool)
	DECLARE_MESSAGE_MAP()

public:

	// Standard constructor/destructor
	CAnnotationTool(CWnd* pParent);
	virtual ~CAnnotationTool();

	// Public members
	bool isRender;

	// Public methods
	BOOL Create();
	afx_msg void OnSelectInImageList();
	afx_msg void OnBnClickedNextImage();
	afx_msg void OnBnClickedPreviousImage();
	afx_msg void OnBnClickedLastImage();
	afx_msg void OnBnClickedFirstImage();
	afx_msg void OnBnClickedBrowse();
	afx_msg void OnBnClickedClear();
	afx_msg void OnBnClickedSaveLog();
	afx_msg void OnBnClickedLoadLog();
	afx_msg void OnBnClickedRadioAnnd();
	afx_msg void OnBnClickedRadioNonAnnd();
	afx_msg void OnBnClickedRadioAll();
	afx_msg void OnBnClickedPastePrevious();
	afx_msg void OnBnClickedUpdate();
	afx_msg void OnBnClickedCheckFit();
	afx_msg void OnBnClickedCheckRender();
	afx_msg void OnBnClickedCheckAutoSave();
	afx_msg void OnTcnSelchangeTabRichedit(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnTcnSelchangeTabRichedit(/* OVERLOADED TO TAKE NO ARGUMENTS */);
	afx_msg void OnCbnSelchangeSortListComboBox();

protected:

	// Protected members
	CWnd*	m_Parent;
	int		m_nID;
	LPRECT dlg_lprect;
	LPRECT img_lprect;
	LPRECT frm_lprect;
	HACCEL m_hAccel;

	// Protected methods
	virtual void DoDataExchange(CDataExchange* pDX);		// DDX/DDV support
	afx_msg void OnClose();

private:
	using CDialog::Create;

	// Private members
	unsigned DLG_DIMX;							// Dialog width
	unsigned DLG_DIMY;							// Dialog height
	unsigned IMG_DIMX;							// Image display width
	unsigned IMG_DIMY;							// Image display height
	unsigned FRM_DIMX;							// Parent frame display width
	unsigned FRM_DIMY;							// Parent frame display height
	CBasicFunction bf;									// Basic functions object
	bool imagesLoaded;									// Images are loaded?
	bool logFileLoaded;									// Log file is loaded?
	std::list<FILELIST> tree;							// Directory tree
	int m_Cur;											// Current position
	int m_TabLastSel;									// Last tab selected
	unsigned m_Count;									// Current count
	unsigned m_Total;									// Total count
	std::vector<std::string> m_FileList;				// Vector of file paths
	std::vector<std::string> m_FileName;				// Vector of file names
	std::vector<__time64_t> m_FileModified;				// Vector of last modified dates
	std::vector<bool> m_FileStatus;	// Redundant		// True = new, false = old
	std::vector<bool> m_DispStatus;	// Redundant		// True = displayed, false = not displayed
	std::vector<int> m_Enum;							// Enumerates every image for caption referencing
	std::vector<int> m_Normal;							// Stores the original order of the items
	CString m_LastContent;								// Stores last annotation in temp clipboard
	enum browsingModes {New,Old,Annd,NonAnnd,All};		// Browsing modes
	browsingModes browsingMode;							// Browsing mode
	enum tabModes {Caption,Reference,Reminds};			// Tab control modes
	enum sortModes {byDirStruct,byAlphabet,byModified,byCaption};	// Combo box modes
	enum { IDD = IDD_DIALOG_ANNOTATION };				// Dialog Data

	// Private methods
	//BOOL PreTranslateMessage(MSG* pMsg);						// OVERLOAD
	void LoadAFolder(CString m_FolderPath);						// Load a folder
	void LoadLogFile();											// Load contents of log file into image list
	void CreateLogFile(std::vector<std::string> m_FileList);	// Create a log file of current images displayed
	void BuildFileList(std::string rootDir, std::list<FILELIST>& tree);	// Build the list of images
	void SortFileList(sortModes SORT_OPTION, size_t range);		// Sort the list of images
	void UpdateTextEntry();										// Check the status of the checkbox
	void FocusOnRichEdit();										// Take focus on rich edit box
	void UpdateList();											// Updates list each time a new image is chosen
	void OnSaveLogFile();										// Save log file
	void OpenRichEdit(CString m_FilePath);						// Open the text file in the RichEdit2
	void TabRicheditNextTab();									// Go to next tab

public:
	//Text Processing
	CTextProcess* textprocess;
	CSearchImage* searchimage;
	afx_msg void OnBnClickedButtonTextprocess();
	//Get the file list
	vector<string>	GetFileList(void);
	//Append keywords
	void	AppendKeyword(string keyword);
	void	DeleteKeyword();
	afx_msg void OnBnClickedImageannotationSavetodatabase();
	afx_msg void OnBnClickedImageannotationSearchimage();
	void SetSearch(void);
};
